package top.i51213.fastaction.action.zip;

import android.content.Context;
import android.net.Uri;
import android.view.View;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import top.i51213.fastaction.action.BaseActonComponent;
import top.i51213.fastaction.action.Data;
import top.i51213.fastaction.action.DataType;
import top.i51213.fastaction.action.IntentType;
import top.i51213.fastaction.uri.MimeType;
import top.i51213.fastaction.R;
import top.i51213.fastaction.uri.UriReader;
import top.i51213.fastaction.uri.UriWriter;
import top.i51213.fastaction.util.IOUtil;

public class ZipCompressActonComponent extends BaseActonComponent {
    public ZipCompressActonComponent(Context context) {
        super(context);
    }

    @Override
    public Data makeOutputData() {
        Data outputData = new Data(DataType.ZipFile);
        List<Uri> list = getInputData().getUris();
        try {
            String fileName = list.size() == 1 ? new UriReader(getContext(), list.get(0)).getDisPlayName() : IOUtil.getLongTimeString();
            UriWriter uriWriter = new UriWriter(getContext(), fileName + ".zip", MimeType.Others.Any, true);
            ZipOutputStream zos = new ZipOutputStream(uriWriter.openOutputStream());
            DisplayNameChecker checker=new DisplayNameChecker();
            for (Uri uri : list) {
                UriReader uriReader = new UriReader(getContext(), uri);
                toZip(uriReader, zos,checker);
            }
            zos.close();
            outputData.addData(DataType.ZipFile, uriWriter.getUri());
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
        return outputData;
    }

    @Override
    public IntentType getCurrentIntentType() {
        return IntentType.ZipCompress;
    }

    @Override
    public DataType getOutputDataType() {
        return DataType.ZipFile;
    }

    public void toZip(UriReader reader, ZipOutputStream zos,DisplayNameChecker checker) throws IOException {
        zos.putNextEntry(new ZipEntry(checker.check(reader.getDisPlayName())));
        InputStream in = reader.openInputStream();
        IOUtil.copyTo(in, zos);
        zos.closeEntry();
        in.close();
    }

    @Override
    public int getImageResId() {
        return R.mipmap.zip;
    }

    @Override
    public String getTitle() {
        return "zip压缩";
    }

    @Override
    public String getInfo() {
        return "";
    }

    @Override
    public View getEditView() {
        return null;
    }

    @Override
    public void editViewDismissListener() {

    }

    static class DisplayNameChecker{
        private final List<String> list=new ArrayList<>();
        public String check(String displayName){
            if(list.contains(displayName)){
                displayName=IOUtil.getFileNameNoExt(displayName)+IOUtil.getRandomTime()+IOUtil.getExt(displayName);
            }
            list.add(displayName);
            return displayName;
        }
    }
}
